<?php

namespace app\admin\controller;

use app\common\model\ApplySales;
use app\common\model\Feedback;
use app\common\model\User as ModelUser;

class User extends Base
{
	public function __construct()
	{
		parent::__construct();
		$this->isLogin();
	}

	/**
	 * @route('user/userlist')
	 * 用户列表
	 */
	public function userlist()
	{
		// 获取搜索条件
		$get['utype'] = input('get.utype');
		$get['is_sales'] = 1;

		$ModelUser = new ModelUser();
		$field = "user_id,utype,nick_name,loginip,is_sales,avatar,lastlogin";

		$rs_data = $ModelUser->get_list_page($get,'user_id desc',$field,$this->limit);

		$tvar = array(
			"title" => "用户列表",
			"time"  => time(),
			"data"  => $rs_data,
			"get"   => $get,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign('page', $page);
		return $this->fetch('user/userlist');
	}

	/**
	 * @route('user/UpgradeSales')
	 * 用戶升级成团长
	 */
	public function UpgradeSales(){
		$user_id = input('post.id');
		$data = [
			'is_sales' => 2,
			'sales_time' => time(),
		];
		$where = [
			'user_id' => $user_id
		];

		$ModelUser = new ModelUser();
		$rs = $ModelUser->save($data,$where);
		if($rs){
			$this->success('操作成功');
		}
		$this->error('操作失败');
	}

	/**
	 * @route('user/saleslist')
	 * 团长列表
	 */
	public function saleslist()
	{
		// 获取搜索条件
		$ModelUser = new ModelUser();

        $field = "user_id,utype,nick_name,loginip,is_sales,avatar,lastlogin";

        $cond['is_sales'] = 2;
        $rs_data = $ModelUser->get_list_page($cond, 'user_id desc',$field,$this->limit);

		$tvar = array(
			"title" => "团长列表",
			"time"  => time(),
			"data"  => $rs_data,
		);
		$page = $rs_data->render();
		$this->assign("data", $tvar);
		$this->assign('page', $page);
		return $this->fetch('user/saleslist');
	}

    /**
     * @route('applysales/applylist')
     * 用户申请销售员列表
     */
    public function applylist()
    {
//        // 获取搜索条件
        $get['nick_name'] = input('get.nick_name');
        $get['status'] = input('get.status');
        $get['pay_status'] = input('get.pay_status');
//        var_dump(345);die;
        $model = new ApplySales();
        $rs_data = $model->admin_get_list($get,'as.add_time desc','as.*,u.nick_name,u.avatar',$this->limit);

        $tvar = array(
            "title" => "用户申请销售员列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
            "statuslist" => $model::$statuslist,
            "paystatuslist" => $model::$paystatuslist,
        );
//        var_dump(345);die;
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('applysales/applylist');
    }

    /**
     * @route('applysales/boxcheck')
     * 用户销售员申请审核
     */
    public function BoxCheck()
    {
        $model = new ApplySales();
        $get['applysales_id'] = input('applysales_id');
//        var_dump($get['applysales_id']);die;
        $checkdetail = $model->where('applysales_id','=',$get['applysales_id'])->find();
//        var_dump($checkdetail);
        if(!$checkdetail){
            $this->error('不存在的审核','/applysales/applylist');
        }
        if(request()->isAjax()){
            $param = request()->param();
            if(empty($param['applysales_id']) || empty($param['status']) || empty($param['reasons'])){
                $this->error('必填项不能为空');
            }
            $data = [
                'status' => $param['status'],
                'reasons' => $param['reasons'],
            ];
//            var_dump($param);die;
            $model->startTrans();
            $rs = $model->edit($data,$param['applysales_id']);
            if($rs === false){
                $this->error($model->getError());
                $model->rollback();
            }
            if($param['status'] == 2){
                $userModel = new ModelUser();
                $where = ['user_id' => $checkdetail['user_id']];
                $urs = $userModel->allowField(true)->save(['is_sales' => 2], $where);
                if($urs === false){
                    $this->error($userModel->getError());
                    $model->rollback();
                }
            }
            $model->commit();
            $this->success('操作成功','/applysales/applylist');
        }
        $tvar = array(
            "title" => "销售员申请审核",
            "time"  => time(),
            "applysales_id" => $get['applysales_id'],
        );
        $this->assign("data", $tvar);
        return $this->fetch('applysales/boxcheck');
    }

    /**
     * @route('feedback/feedbacklist')
     * 用户反馈列表
     */
    public function feedbacklist()
    {
//        // 获取搜索条件
        $get['nick_name'] = input('get.nick_name');
//        var_dump(345);die;
        $model = new Feedback();
        $rs_data = $model->admin_get_list($get,'f.add_time desc','f.*,u.nick_name,u.avatar',$this->limit);

        $tvar = array(
            "title" => "用户反馈列表",
            "time"  => time(),
            "data"  => $rs_data,
            "get"   => $get,
        );
//        var_dump(345);die;
        $page = $rs_data->render();
        $this->assign("data", $tvar);
        $this->assign('page', $page);
        return $this->fetch('feedback/feedbacklist');
    }
}